#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#include <linux/input.h>


#include "input_ts.h"


//获取用户手指的滑动方向的
int get_finger_move_dir(void)
{	
	int fd;

	fd = open("/dev/event0", O_RDONLY);
	if (fd == -1)
	{
		printf("failed to open /dev/event0\n");
		return -1;
	}

	int x1 = -1, y1 = -1;		 	//一次滑动过程中起点的坐标，同时判断是否是第一次按下
	int x2, y2; 					//一次滑动过程中终点的坐标
	struct input_event ev; 			//输入事件


	while (1)
	{
		int r = read(fd, &ev, sizeof(ev));
		if (r != sizeof(ev))
		{
			continue;
		}

		//X轴坐标事件 ev.value就是x轴坐标
		if (ev.type == EV_ABS && ev.code == ABS_X)
		{
			if (x1 == -1)
			{
				x1 = ev.value;
			}
			x2 = ev.value;
		}


		if (ev.type == EV_ABS && ev.code == ABS_Y)
		{
			if (y1 == -1)
			{
				y1 = ev.value;
			}
			y2 = ev.value;
		}


		if (ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == 0)
		{
			int delt_x =abs( x2 - x1);
			int delt_y = abs(y2 - y1);
			if (delt_y > 2 * delt_x)
			{
				if (y2 > y1)
				{
					close(fd);
					return MOVE_DOWN;
				}
				else
				{
					close(fd);
					return MOVE_UP;
				}
			}
			else if (delt_x > 2*delt_y)
			{
				//do it yourself ... please
				if(x2>x1)
				{
				close(fd);
				return MOVE_RIGHT;
				}
				else
				{
				close(fd);
				return MOVE_LEFT;
				}
					
			}
			 
			else
			{
				//方向不明确，请再滑动一次
				x1 = -1;
				y1 = -1;
			}
		}
	}





	close(fd);


}
